<template>
	<view>
		<u-sticky>
			<scan></scan>
			<scanner></scanner>
			<sm></sm>
			<honeywell></honeywell>
			<hk></hk>
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs>
		</u-sticky>
		<u-card v-if="current == 0">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<view class="u-text-center u-m-t-20 weikaishi3 huanhang">请选择库区</view>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<view class="u-m-20">
					<u-row>
						<u-col span="12" text-align="center">
							<view class="u-m-t-20">
								<u-radio-group v-model="areaName">
									<u-radio label-size="50rpx" shape="circle" @change="radioChange"
										v-for="(item, index) in areaList" :key="index" :name="item.name"
										iconPlacement="right">
											{{item.name}}
									</u-radio>
								</u-radio-group>
							</view>
						</u-col>
					</u-row>
				</view>
				<!-- 分隔线 -->
				<view class="divider"></view>
				
				<!-- 输入数量区域 -->
				<view class="u-m-20">
					<u-row>
						<u-col span="4" text-align="right">
							<view class="u-m-t-20">
								<view class="input-label">下发数量:</view>
							</view>
						</u-col>
						<u-col span="8">
							<view class="u-m-t-20">
								<input type="number" v-model="sendingNum" placeholder="请输入下发数量" class="input-box" />
							</view>
						</u-col>
					</u-row>
				</view>
				<view class="divider"></view>
				<!-- 新增的下发任务按钮 -->
				<button class="issue-button" @click="issueTask">下发任务</button>
			</view>
		</u-card>
		<u-card v-if="current == 0">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="2" text-align="center">
							<view class=" weikaishi">
								<span>序号</span>
							</view>
						</u-col>
						<u-col span="7" text-align="center">
							<view class=" weikaishi">
								<span>库区</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class=" weikaishi">
								<span>未下发</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-row v-for="(item, index) in storageDataList" :key="index">
					<u-col span="2" text-align="center">
						<view class="lanse u-m-t-10 huanhang">
							<span>{{ index + 1 }}</span>
						</view>
					</u-col>
					<u-col span="7" text-align="center">
						<view class="u-m-t-10 huanhang">
							<span>{{item.areaName}}</span>
						</view>
					</u-col>
					<u-col span="3" text-align="center">
						<view class="u-m-t-10 huanhang">
							<span>{{item.storageOutCnt}}</span>
						</view>
					</u-col>
				</u-row>
			</view>
		</u-card>
		<u-card v-if="current == 1">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20">
				<u-search placeholder="请输入关键词搜索" :clearabled="true" v-model="keyword" border-color="#606266"
					@custom="search" @search="search"></u-search>
			</view>
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="4" text-align="center">
							<view class=" weikaishi">
								<span>客户</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class=" weikaishi">
								<span>单号</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class=" weikaishi">
								<span>领取人</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-row v-for="(item,index) in dataList2" :key="index" @click="toput(item.id,item.storageUser)">
					<u-col span="4" text-align="center">
						<view class="lanse u-m-t-10 huanhang">
							<span>{{item.getUnit}}</span>
						</view>
					</u-col>
					<u-col span="4" text-align="center">
						<view class="u-m-t-10 huanhang">
							<span>{{item.storageNumber}}</span>
						</view>
					</u-col>
					<u-col span="4" text-align="center">
						<view class="u-m-t-10 huanhang">
							<span>{{item.storageUserName}}</span>
						</view>
					</u-col>
					<u-col span="12" text-align="center">
						<view class="jiange">
						</view>
					</u-col>
				</u-row>
			</view>
		</u-card>
	</view>
</template>

<script>
	
	import scan from "@/components/uni-scan/scan.vue";
	import scanner from "@/components/uni-scan/scanner.vue";
	import sm from "@/components/uni-scan/sm.vue";
	import honeywell from "@/components/uni-scan/honeywell.vue";
	
	export default {
		components: {
			scanner,
			scan,
			sm,
			honeywell
		},
		data() {
			return {
				current: 0,
				list: [{
						name: '接单大厅'
					},
					{
						name: '已领取'
					}
				],
				areaList: [],
				// u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
				areaName: '一区',
				dataList2: [{
					getUnit:'门店1',
					storageNumber:'JH0000000001',
					storageUserName:'张三'
				},{
					getUnit:'门店2',
					storageNumber:'JH0000000002',
					storageUserName:'李四'
				}],
				storageDataList: [],
				keyword:'',
				sendingNum: 1,
			}
		},
		onShow() {
			let that = this
			uni.$off('scan');
			uni.$on('scan', (res) => {
				console.log('扫码结果：', res.codeStr);
				that.scanAgv(res.codeStr);
			});
			uni.$off('scanner') // 每次进来先 移除全局自定义事件监听器
			uni.$on('scanner', (res) => {
				console.log('扫码结果：', res.code);
				that.scanAgv(res.code);
			});
			uni.$off('sm') // 每次进来先 移除全局自定义事件监听器
			uni.$on('sm', (res) => {
				console.log('扫码结果：', res.code);
				that.scanAgv(res.code);
			});
			uni.$off('honeywell') // 每次进来先 移除全局自定义事件监听器
			uni.$on('honeywell', (res) => {
				console.log('扫码结果：', res.code);
				that.scanAgv(res.code);
			});
			
			this.getList2();
			this.getStorageDataList();
		},
		onPullDownRefresh() {
			if(this.current == 1){
				this.getList2();
			}else{
				this.getStorageDataList();
			}
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
			this.$http.post('/rest/storage/Arealist', {}, {
				params: {},
				header: {
					'Authorization': uni.getStorageSync('token'),
				}
			}).then(res => {
				this.areaName = res.data.data[0].areaName
				for (let item1 of res.data.data) {
					let obj = {}
					obj.name = item1.areaName
					this.areaList.push(obj)
				}
			})
		},
		methods: {
			// 选中某个单选框时，由radio时触发
			radioChange(e) {
				// console.log(e);
			},
			sectionChange(e) {
				this.current = e;
				if(this.current == 1){
					this.getList2();
				}else{
					this.getStorageDataList();
				}
			},
			issueTask() {
				uni.showModal({
					title: '确认下发AGV任务吗?',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/agvStorageScan/issueTask', {}, {
								params: {
									areaName: this.areaName,
									sendingNum: this.sendingNum,
								},
								header: {
									'Authorization': uni.getStorageSync('token'),
								}
							}).then(res => {
								if (res.data.code == 200) {
									uni.showToast({
										icon: 'none',
										title: '下发成功',
										duration: 2000
									});
									this.getStorageDataList();
								} else {
									uni.showToast({
										icon: 'none',
										title: res.data.message,
										duration: 2000
									});
									
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			toput(id, storageUser) {
				this.$http.post('/rest/hxStorageOffShelf/findUser2', {}, {
					params: {
						account: storageUser
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					if (res.data.code == 200) {
						this.$Router.push({
							name: 'hx_storage_agv_scan_detail',
							params: {
								id: id
							},
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.message,
							duration: 2000
						});
					}
				})
			},
			getList2(){
				uni.showLoading({
					title: '加载中'
				});
				this.$http.post('/rest/HxStorageOut/appfindyilingqu', {}, {
					params: {
						keyword: this.keyword
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading();
					this.dataList2 = res.data.data
					this.list[1].name = '已领取(' + res.data.data.length + ')'
				})
			},
			getStorageDataList(){
				uni.showLoading({
					title: '加载中'
				});
				this.$http.post('/rest/agvStorageScan/areaStorageOutCntList', {}, {
					params: {},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					uni.hideLoading();
					if(res.data && res.data != ""){
						this.storageDataList = res.data
						this.list[1].name = '已领取(' + res.data.length + ')'
					}else{
						this.list[1].name = '已领取(0)'
					}
					
				})
			},
			scanAgv(agvNo){
				this.$http.post('/rest/agvStorageScan/scanAgv', {}, {
					params: {
						agvNo: agvNo,
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					if (res.data.code == 200) {
						uni.showToast({
							icon: 'none',
							title: '领取成功',
							duration: 2000
						});
						this.$Router.push({
							name: 'hx_storage_agv_scan_detail',
							params: {
								id: res.data.data
							},
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.message,
							duration: 2000
						});
						
					}
				})
			},
			search(e) {
				this.getList2();
			}
		}
	}
</script>


<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}

	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}

	.weikaishi2 {
		font-size: 34rpx;
	}

	.weikaishi3 {
		font-size: 30rpx;
	}

	.jiange {
		border-bottom: 1px solid #ebebeb;
	}

	.huanhang {
		word-break: break-all;
	}

	.baise {
		background-color: #2979ff;
		color: #ffffff;
		font-size: 45rpx;
	}
	
	.input-box {
		width: 100%;
		height: 60rpx;
		border: 1px solid #ddd;
		border-radius: 8rpx;
		padding: 0 15rpx;
		box-sizing: border-box;
		font-size: 28rpx;
	}
	
	/* 分隔线样式 */
	.divider {
		width: 100%;
		height: 1px;
		background-color: #ebebeb;
		margin: 20rpx 0;
	}
	
	/* 新增的按钮样式 */
	.issue-button {
		width: 100%;
		height: 80rpx;
		background-color: #2979ff;
		color: white;
		border: none;
		border-radius: 8rpx;
		font-size: 32rpx;
		margin: 20rpx 0;
	}
</style>